package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.PageResult;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.TaskMapper;
import com.heima.mapper.UserMapper;
import com.heima.mapper.VendingMachineMapper;
import com.heima.pojo.*;
import com.heima.pojo.dto.TaskDetailsDto;
import com.heima.pojo.dto.TaskDto;
import com.heima.pojo.vo.TaskVo;
import com.heima.pojo.vo.UserWorkVo;
import com.heima.service.ITaskDetailsService;
import com.heima.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private ITaskDetailsService taskDetailsService;

    /**
     * 工单查询
     *
     * @param start     开始时间
     * @param end       结束时间
     * @param innerCode 设备编号
     * @param userId    执行人Id
     * @param taskCode  工单编号
     * @param status    工单状态
     * @param isRepair  是否是维修工单
     * @return
     */
    @Override
    public PageResult<TaskVo> search(PageResult<TaskVo> pageResult, String start, String end, String innerCode, Integer userId, String taskCode, String status, Boolean isRepair) {
        Page<TaskVo> page = new Page<>(pageResult.getPageIndex(), pageResult.getPageSize());
        taskMapper.search(page, start, end, innerCode, userId, taskCode, status, isRepair);
        return pageResult.of(page, TaskVo.class);
    }

    /**
     * 工单详情
     *
     * @param taskId 工单Id
     * @return
     */
    @Override
    public TaskVo taskInfo(Long taskId) {
        return taskMapper.taskInfo(taskId);
    }

    /**
     * 工单统计
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return
     */
    @Override
    public List<TaskReportInfo> taskReportInfo(String start, String end) {
        TaskReportInfo taskReportInfo = taskMapper.taskReportInfo(start, end, false);
        taskReportInfo.setRepair(false);
        List<TaskReportInfo> list = new ArrayList<>();
        list.add(taskReportInfo);
        TaskReportInfo taskReportInfo1 = taskMapper.taskReportInfo(start, end, true);
        taskReportInfo1.setRepair(true);
        list.add(taskReportInfo1);
        return list;
    }

    @Override
    @Transactional
    public Boolean create(TaskDto taskDto) {
        User user = userMapper.selectById(taskDto.getUserId());
        LambdaQueryWrapper<VendingMachine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VendingMachine::getInnerCode, taskDto.getInnerCode());
        VendingMachine vendingMachine = vendingMachineMapper.selectOne(queryWrapper);
        Node node = nodeMapper.selectById(vendingMachine.getNodeId());
        taskDto.setTaskCode(String.valueOf(System.currentTimeMillis()));
        taskDto.setTaskStatus(1);
        taskDto.setUserName(user.getUserName());
        taskDto.setRegionId(user.getRegionId());
        taskDto.setAddr(node.getAddr());
        taskDto.setUpdateTime(LocalDateTime.now());
        taskDto.setCreateTime(LocalDateTime.now());
        Task bean = BeanUtil.toBean(taskDto, Task.class);
        bean.setProductTypeId(taskDto.getProductType());
        save(bean);
        if (ObjectUtil.isNotEmpty(taskDto.getDetails())) {
            List<TaskDetailsDto> details = taskDto.getDetails();
            details.forEach(item -> {
                item.setTaskId(taskDto.getTaskId());
            });
            return taskDetailsService.saveBatch(BeanUtil.copyToList(details, TaskDetails.class));
        }
        return true;
    }

    @Override
    public List<CollectReport> collectReport(String start, String end) {
        return taskMapper.collectReport(start, end);
    }

    @Override
    public List<Map<String, Object>> userWorkTop10(String start, String end, Boolean isRepair, Long regionId) {
        List<Map<String, Object>> list = taskMapper.userWorkTop10(start, end, isRepair, regionId);
        return list.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public UserWorkVo userWork(String start, String end, Integer userId) {
        return taskMapper.userWork(start, end, userId);
    }
}
